package com.yanqu.road.dao.impl.hunt;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.message.WorldMessage;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HuntMessageDaoImpl extends TempDataObjectDao<WorldMessage> {

    public List<WorldMessage> getHuntMessageList(long selectTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<WorldMessage> dataList = new ArrayList<>();
        String sql = "select * from t_u_hunt_message where sendTime>? order by sendTime";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, selectTime);
            rs = call.executeQuery();
            while (rs.next()) {
                WorldMessage data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    @Override
    protected boolean addData(WorldMessage data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_hunt_message(type,sendTime,content,userId) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getType());
            call.setLong(i++, data.getSendTime());
            call.setString(i++, data.getContent());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(WorldMessage data) {
        return false;
    }

    public boolean deleteHuntMessage(long deleteTime) {
        PreparedStatement call = null;
        String sql = "delete from t_u_hunt_message where sendTime<?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, deleteTime);
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected WorldMessage parseData(ResultSet rs) throws SQLException {
        WorldMessage data = new WorldMessage();
        data.setType(rs.getInt("type"));
        data.setSendTime(rs.getLong("sendTime"));
        data.setContent(rs.getString("content"));
        data.setUserId(rs.getLong("userId"));
        return data;
    }
}
